package cn.dhnep.config;

import cn.dhnep.demo.pojo.Admin;
import cn.dhnep.demo.util.Constant;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MyInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String uri = request.getRequestURI();

        //包含login则直接放行,
        if (uri.indexOf("login") > 0) {
            return true;
        }
        if (uri.indexOf("logout") > 0){
            request.getRequestDispatcher("/page/login-3.html").forward(request, response);
        }

        Admin userSession = (Admin) request.getSession().getAttribute(Constant.USER_SESSION);

        if (null != userSession) {
            return true;
        }

        request.getRequestDispatcher("/page/login-3.html").forward(request, response);
        return false;
    }
}
